package org.iwidget.desktop.core;
import org.iwidget.desktop.model.*;
import java.awt.Point;
import java.util.*;
/**
 *
 * @author Muhammad Hakim A
 */


public class IwidgetMouseLoop
{
    class MyTimerTask extends TimerTask
    {

        public void run()
        {
            //parent.indicator.getMousePos(p);
            if(p.x != mouseX || p.y != mouseY)
            {
                mouseX = p.x;
                mouseY = p.y;
                triggerEvent("onSystemMouseMove");
            }
        }

        MyTimerTask()
        {
        }
    }


    public IwidgetMouseLoop(Iwidget parent, Hashtable widgetList)
    {
        p = new Point();
        triggering = false;
        this.widgetList = widgetList;
        this.parent = parent;
    }

    public void triggerEvent(String actionType)
    {
        if(!triggering)
        {
            triggering = true;
            Enumeration enum1 = widgetList.keys();
            do
            {
                if(!enum1.hasMoreElements())
                    break;
                String key = (String)enum1.nextElement();
                WidgetElement widget = (WidgetElement)widgetList.get(key);
                ActionElement action = widget.getAction(actionType);
                if(action != null)
                    try
                    {
                        String script = action.getScript();
                        if(actionType.equals("onSystemMouseMove") && widget.elementWindow.getName() == null)
                            script = null;
                        if(script != null)
                            widget.executeScript(widget, action.getScript(), action.getType());
                        else
                        if(action.getFile() != null)
                            widget.executeScriptFile(widget, action.getFile());
                        script = null;
                    }
                    catch(Exception e)
                    {
                        e.printStackTrace();
                    }
            } while(true);
        }
        triggering = false;
    }

    Iwidget parent;
    Hashtable widgetList;
    public int mouseX;
    public int mouseY;
    Point p;
    boolean triggering;
}